package com.inifitness.common.thread;

import java.util.concurrent.LinkedBlockingDeque;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

import org.springframework.boot.SpringBootConfiguration;
import org.springframework.context.annotation.Bean;
import org.springframework.scheduling.annotation.EnableAsync;

/**
 * 线程池配置
 *
 * @author sunjinfeng
 * @version 1.0
 * @since 2024/1/22 13:24
 */

@SpringBootConfiguration
@EnableAsync
public class ThreadPoolConfig {

  /**
   * 这里定义一个日志追踪线程池，使用时直接注入ThreadPoolTaskExecutor，使用@Qualifier("getLogTraceExecutor")指定bean
   */
  @Bean
  public ThreadPoolExecutor getLogTraceExecutor() {
    ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(
        2,
        5,
        3,
        TimeUnit.SECONDS,
        new LinkedBlockingDeque<>(10),
        new NamedThreadFactory("pool"),
        new ThreadPoolExecutor.AbortPolicy());
    return threadPoolExecutor;
  }
}
